package com.linkwedo.saas.support;

import java.util.ArrayList;
import java.util.Map;

import javax.servlet.ServletContext;

/**
 * 在application  scope内维护已创建连接池的公司
 * @author rabbit
 *
 */
@SuppressWarnings("unchecked")
public class JNDIURLList {
	
	private JNDIURLList() {
		throw new AssertionError();
	}
	
	public static boolean isAdded(String url, Map<String, Object> application) {
		synchronized (application) {
			Object o = application.get(Constants.APPJNDIURLKEY);
			if (o == null) {
				application.put(Constants.APPJNDIURLKEY, new ArrayList<String>());
				return false;
			}
			ArrayList<String> list = (ArrayList<String>)o;
			if (list.contains(url)) {
				return true;
			}
			return false;
		}
	}
	
	public static boolean isAdded(String url, ServletContext context) {
		synchronized (context) {
			Object o = context.getAttribute(Constants.APPJNDIURLKEY);
			if (o == null) {
				context.setAttribute(Constants.APPJNDIURLKEY, new ArrayList<String>());
				return false;
			}
			ArrayList<String> list = (ArrayList<String>)o;
			if (list.contains(url)) {
				return true;
			}
			return false;
		}
	}

	public static void add(String jndiURL, Map<String, Object> application) {
		synchronized (application) {
			Object o = application.get(Constants.APPJNDIURLKEY);
			ArrayList<String> list = (ArrayList<String>)o;
			list.add(jndiURL);
		}
	}
	
	public static void add(String jndiURL, ServletContext context) {
		synchronized (context) {
			Object o = context.getAttribute(Constants.APPJNDIURLKEY);
			ArrayList<String> list = (ArrayList<String>)o;
			list.add(jndiURL);
		}
	}
}
